package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ROB_CART, desc = "劫镖")
public class BeautyEscortRobCartCmd extends AbstractCommand {

    long userId;
    long serverId;
    long dartId;
    List<Integer> beautyList;
    int position;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortRobCartReqMsg reqMsg = BeautyEscortProto.BeautyEscortRobCartReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        serverId = reqMsg.getServerId();
        dartId = reqMsg.getDartId();
        beautyList = reqMsg.getBeautyList();
        position = reqMsg.getPosition();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        //商会等级限制
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
        if(UnionConfigMgr.getUnionLv(unionInfo.getExperience()) < GameConfig.ESCORT_OPEN_UNION_LEVEL) {
            ret = GameErrorCode.E_BEAUTY_ESCORT_UNION_LEVEL_NO_ENOUGH;
            return;
        }
        if(userId <= 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        if(userId == player.getUserId()){
            ret = GameErrorCode.E_BEAUTY_ESCORT_CAN_NOT_ROB_SELF_OR_UNION;
            return;
        }
        if(serverId == GameServer.getInstance().getServerId()){
            UserInfo enemy = UserMgr.getUserInfo(userId);
            if(enemy != null && player.getUserInfo().getUnionUid().equals(enemy.getUnionUid())){
                ret = GameErrorCode.E_BEAUTY_ESCORT_CAN_NOT_ROB_SELF_OR_UNION;
                return;
            }
        }
        if (player.isRequestToFast()) {
            ret  =  GameErrorCode.E_REQUEST_TOO_FAST;
            return;
        }
        ret = player.getModule(BeautyEscortModule.class).rob(serverId, userId, dartId, beautyList, position);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortRobCartRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortRobCartRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ROB_CART, respMsg);
        }
    }
}
